<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>权限添加</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <label class="layui-form-label required">类型</label>
        <div class="layui-input-block" id="pType">
            <input type="radio" name="type" value="0" title="目录" lay-filter="typeRadio" checked>
            <input type="radio" name="type" value="1" title="菜单" lay-filter="typeRadio">
            <input type="radio" name="type" value="2" title="内页" lay-filter="typeRadio">
            <input type="radio" name="type" value="3" title="接口" lay-filter="typeRadio">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">父级</label>
        <div class="layui-input-block">
            <select name="pid" lay-verify="required" lay-reqtext="父级必选">
                <option value="">-- 请选择父级 --</option>
                <option value="0">根（Root）</option>
                {% for val in permissions %}
                    <option value="{{ val.id }}">{{ val.name }}</option>
                {% endfor %}
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">权限名称</label>
        <div class="layui-input-block">
            <input type="text" name="name" lay-verify="required" lay-reqtext="权限名称必填" placeholder="请输入权限名" value=""
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">唯一标识</label>
        <div class="layui-input-block">
            <input type="text" name="authority" lay-verify="required" lay-reqtext="唯一标识必填" placeholder="请输入唯一标识"
                   value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item type-menu" style="display: none">
        <label class="layui-form-label required">菜单icon</label>
        <div class="layui-input-block" id="menu_radio">
            <input type="text" name="menu_icon" placeholder="请输入菜单icon" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item type-uri" style="display: none">
        <label class="layui-form-label required">请求路径</label>
        <div class="layui-input-block">
            <input type="text" name="request_uri" placeholder="请输入权限名" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item type-api" style="display: none">
        <label class="layui-form-label required">请求方式</label>
        <div class="layui-input-block">
            <select name="request_method">
                <option value="">-- 请选择请求方式 --</option>
                <option value="GET">GET</option>
                <option value="POST">POST</option>
                <option value="PUT">PUT</option>
                <option value="DELETE">DELETE</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">排序号</label>
        <div class="layui-input-block">
            <input type="number" name="sort" placeholder="请输入排序号" value="" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
<script src="/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;

        form.on('radio(typeRadio)', function () {
            var selectedTypeVal = $('#pType input[name="type"]:checked').val();
            if (selectedTypeVal == 0) {        // 目录
                $('.type-menu').show();
                $('.type-uri').hide();
                $('.type-api').hide();
            } else if (selectedTypeVal == 1) { // 菜单
                $('.type-menu').show();
                $('.type-uri').show();
                $('.type-api').hide();
            } else if (selectedTypeVal == 2) { // 按钮
                $('.type-uri').show();
                $('.type-menu').hide();
                $('.type-api').hide();
            } else {                            // 接口
                $('.type-uri').show();
                $('.type-menu').hide();
                $('.type-api').show();
            }
            return false;
        });

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            $.ajax({
                type: "POST",
                cache: false,
                url: '/admin/permission/store',
                data: data.field,
                success: function (res) {
                    if (res.state === 'success') {
                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                    } else {
                        var index = layer.alert(res.msg, {icon: 0, shade: 0.6}, function () {
                            // 关闭弹出层
                            layer.close(index);
                        });
                    }
                },
                error: function () {
                    layer.alert('操作失败，请联系管理员', {icon: 0, shade: 0.6});
                }
            });
            return false;
        });

    });
</script>
</body>
</html>